//
//  ShouYeViewController.swift
//  ZHM
//  Created by zzddongge on 16/7/20.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

class ShouYeViewController: UIViewController {

    //获取模拟器的宽带和高度
    var screenWidth = UIScreen.main.bounds.width
    var screenHeight = UIScreen.main.bounds.height
    //创建四个按钮:汉字训练、趣味训练、定制方案、系统管理
    var hanZiXunLianBtn:UIButton?
    var quWeiXunLianBtn:UIButton?
    var dingZhiFangAnBtn:UIButton?
    var xiTongGuanLiBtn:UIButton?
    var woYaoXueXiBtn:UIButton?
    
    //创建四个imageView,分别对应于汉字训练、趣味训练、定制方案、系统管理等四个图标
    var shouYeImageView:UIImageView?
    var hanZiXunLianImageView:UIImageView?
    var quWeiXunLianImageView:UIImageView?
    var dingZhiFangAnImageView:UIImageView?
    var xiTongGuanLiImageView:UIImageView?
    var woYaoXueXiImageView:UIImageView?
    
    //创建四个label对象:汉字label、趣味label、定制label、系统label
    var hanZiLabel:UILabel?
    var quWeiLabel:UILabel?
    var dingZhiLabel:UILabel?
    var xiTongLabel:UILabel?
    var woYaoXueXiLabel:UILabel?
    
    //定义三个分割线,分别用于分割不同模块
    var hanZiYouBianFenGeXian:UIView?
    var quWeiXunLianFenGeXian:UIView?
    var xiTongGuanLiFenGeXian:UIView?
    var woYaoXueXiFenGeXian:UIView?
    
    //定义一个横向的分割线,分别用于分割不同模块
    var firstHengXiangFenGeXian:UIView?
    var secondHengXiangFenGeXian:UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "纵横码"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        //获取模拟器导航栏的高度
        let navigationHeight = self.navigationController?.navigationBar.frame.size.height
        let navigationHeights = navigationHeight!+20
        let oneThirdScreenWidth = screenWidth/3
        
        //初始化首页顶端的imageView图片
        shouYeImageView = UIImageView(image: UIImage(named: "ZHHZJXa.02_01.L"))
        shouYeImageView!.frame = CGRect(x: 0, y: navigationHeights, width: screenWidth, height: oneThirdScreenWidth+20)
        
        
        //初始化开始训练按钮
        hanZiXunLianBtn = UIButton(frame:CGRect(x: 0,y: navigationHeights+oneThirdScreenWidth+20,width: oneThirdScreenWidth,height: oneThirdScreenWidth))
        hanZiXunLianImageView = UIImageView(image: UIImage(named: "hanzixunlian"))
        
        //获取汉字训练图标的宽度
        let hanZiXunLianImageViewWidth = getImageViewWidth(hanZiXunLianImageView!)
        let hanZiXunLianImageViewHeight = getImageViewHeight(hanZiXunLianImageView!)
        hanZiXunLianImageView?.frame = CGRect(x: (oneThirdScreenWidth-hanZiXunLianImageViewWidth)/2, y: 20, width: hanZiXunLianImageViewWidth, height: hanZiXunLianImageViewHeight)
        //设置汉字训练label字体
        hanZiLabel = UILabel(frame:CGRect(x: (oneThirdScreenWidth-80)/2,y: 65,width: 80,height: 30))
        hanZiLabel!.text = "汉字训练"
        hanZiLabel?.textAlignment = .center
        setAttribute(hanZiLabel!)
        hanZiXunLianBtn?.addSubview(hanZiXunLianImageView!)
        hanZiXunLianBtn?.addSubview(hanZiLabel!)
        
        
        //初始化趣味训练按钮
        quWeiXunLianBtn = UIButton(frame:CGRect(x: oneThirdScreenWidth,y: navigationHeights+oneThirdScreenWidth+20,width: oneThirdScreenWidth,height: oneThirdScreenWidth))
        //初始化趣味训练图标
        quWeiXunLianImageView = UIImageView(image: UIImage(named: "quweixunlian"))
        let quWeiXunLianImageViewWidth = getImageViewWidth(quWeiXunLianImageView!)
        let quWeiXunLianImageViewHeight = getImageViewHeight(quWeiXunLianImageView!)
        
        quWeiXunLianImageView?.frame = CGRect(x:(oneThirdScreenWidth-quWeiXunLianImageViewWidth)/2,y:20,width:quWeiXunLianImageViewWidth,height:quWeiXunLianImageViewHeight)
        
        //初始化趣味训练label
        quWeiLabel = UILabel(frame:CGRect(x: (oneThirdScreenWidth-80)/2,y: 65,width: 80,height: 30))
        quWeiLabel?.text = "趣味训练"
        quWeiLabel?.textAlignment = .center
        setAttribute(quWeiLabel!)
        quWeiXunLianBtn?.addSubview(quWeiXunLianImageView!)
        quWeiXunLianBtn?.addSubview(quWeiLabel!)
        
        //初始化定制方案按钮
        dingZhiFangAnBtn = UIButton(frame:CGRect(x: oneThirdScreenWidth*2,y: navigationHeights+oneThirdScreenWidth+20,width: oneThirdScreenWidth,height: oneThirdScreenWidth))
        //初始化定制方案imageView
        dingZhiFangAnImageView = UIImageView(image: UIImage(named: "dingzhi"))
        let dingZhiFangAnImageViewWidth = getImageViewWidth(dingZhiFangAnImageView!)
        let dingZhiFangAnImageViewHeight = getImageViewHeight(dingZhiFangAnImageView!)
        
        dingZhiFangAnImageView?.frame = CGRect(x:(oneThirdScreenWidth-dingZhiFangAnImageViewWidth)/2,y:20,width:dingZhiFangAnImageViewWidth,height:dingZhiFangAnImageViewHeight)
        //初始化定制方案label
        dingZhiLabel = UILabel(frame:CGRect(x: (oneThirdScreenWidth-80)/2,y: 65,width: 80,height: 30))
        dingZhiLabel?.text = "定制方案"
        dingZhiLabel?.textAlignment = .center
        setAttribute(dingZhiLabel!)
        dingZhiFangAnBtn?.addSubview(dingZhiFangAnImageView!)
        dingZhiFangAnBtn?.addSubview(dingZhiLabel!)
        
        //初始化系统管理按钮
        xiTongGuanLiBtn = UIButton(frame:CGRect(x: 0,y: navigationHeights+oneThirdScreenWidth*2+20,width: oneThirdScreenWidth,height: oneThirdScreenWidth))
        //初始化系统管理imageView
        xiTongGuanLiImageView = UIImageView(image: UIImage(named: "xitongguanli"))
        let xiTongGuanLiImageViewWidth = xiTongGuanLiImageView?.bounds.size.width
        let xiTongGuanLiImageViewHeight = xiTongGuanLiImageView?.bounds.size.height
        
        xiTongGuanLiImageView?.frame = CGRect(x:(oneThirdScreenWidth-xiTongGuanLiImageViewWidth!)/2,y:20,width:xiTongGuanLiImageViewWidth!,height:xiTongGuanLiImageViewHeight!)
        //初始化系统管理label
        xiTongLabel = UILabel(frame:CGRect(x: (oneThirdScreenWidth-80)/2,y: 65,width: 80,height: 30))
        xiTongLabel?.text = "系统管理"
        xiTongLabel?.textAlignment = .center
        setAttribute(xiTongLabel!)
        xiTongGuanLiBtn?.addSubview(xiTongGuanLiImageView!)
        xiTongGuanLiBtn?.addSubview(xiTongLabel!)
        
        //初始化我要学习按钮
        woYaoXueXiBtn = UIButton(frame:CGRect(x: oneThirdScreenWidth,y: navigationHeights+oneThirdScreenWidth*2+20,width: oneThirdScreenWidth,height: oneThirdScreenWidth))
        //初始化我要学习imageView
        woYaoXueXiImageView = UIImageView(image: UIImage(named: "woyaoxuexi"))
        let dianDianDianImageViewWidth = getImageViewWidth(woYaoXueXiImageView!)
        let dianDianDianImageViewHeight = getImageViewHeight(woYaoXueXiImageView!)
        woYaoXueXiImageView?.frame = CGRect(x:(oneThirdScreenWidth-dianDianDianImageViewWidth)/2,y:20,width:dianDianDianImageViewWidth,height:dianDianDianImageViewHeight)
        woYaoXueXiBtn?.addSubview(woYaoXueXiImageView!)
        //初始化我要学习label
        woYaoXueXiLabel = UILabel(frame:CGRect(x: (oneThirdScreenWidth-80)/2,y: 65,width: 80,height: 30))
        woYaoXueXiLabel?.text = "我要学习"
        woYaoXueXiLabel?.textAlignment = .center
        setAttribute(woYaoXueXiLabel!)
        woYaoXueXiBtn?.addSubview(woYaoXueXiImageView!)
        woYaoXueXiBtn?.addSubview(woYaoXueXiLabel!)
        
        //初始化三个分割线
        hanZiYouBianFenGeXian = UIView(frame:CGRect(x: screenWidth/3,y: navigationHeights+oneThirdScreenWidth+20,width: 1,height: screenWidth/3))
        hanZiYouBianFenGeXian?.layer.borderWidth = 1
        hanZiYouBianFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        
        quWeiXunLianFenGeXian = UIView(frame:CGRect(x: 2*screenWidth/3,y: navigationHeights+oneThirdScreenWidth+20,width: 1,height: screenWidth/3))
        quWeiXunLianFenGeXian?.layer.borderWidth = 1
        quWeiXunLianFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        xiTongGuanLiFenGeXian = UIView(frame:CGRect(x: screenWidth/3,y: navigationHeights+oneThirdScreenWidth*2+20,width: 1,height: screenWidth/3))
        xiTongGuanLiFenGeXian?.layer.borderWidth = 1
        xiTongGuanLiFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        
        firstHengXiangFenGeXian = UIView(frame:CGRect(x: 0,y: navigationHeights+oneThirdScreenWidth*2+20,width: screenWidth,height: 1))
        firstHengXiangFenGeXian?.layer.borderWidth = 1
        firstHengXiangFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        woYaoXueXiFenGeXian = UIView(frame:CGRect(x: 2*screenWidth/3,y: navigationHeights+oneThirdScreenWidth*2+20,width: 1,height: screenWidth/3))
        woYaoXueXiFenGeXian?.layer.borderWidth = 1
        woYaoXueXiFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        
        secondHengXiangFenGeXian = UIView(frame:CGRect(x: 0,y: navigationHeights+oneThirdScreenWidth*3+20,width: 2*screenWidth/3,height: 1))
        secondHengXiangFenGeXian?.layer.borderWidth = 1
        secondHengXiangFenGeXian?.layer.borderColor = UIColor(red: 222/255,green: 222/255,blue: 222/255,alpha: 1).cgColor
        
        
        self.view.addSubview(shouYeImageView!)
        self.view.addSubview(hanZiXunLianBtn!)
        self.view.addSubview(quWeiXunLianBtn!)
        self.view.addSubview(dingZhiFangAnBtn!)
        self.view.addSubview(xiTongGuanLiBtn!)
        self.view.addSubview(hanZiYouBianFenGeXian!)
        self.view.addSubview(quWeiXunLianFenGeXian!)
        self.view.addSubview(xiTongGuanLiFenGeXian!)
        self.view.addSubview(firstHengXiangFenGeXian!)
        self.view.addSubview(woYaoXueXiFenGeXian!)
        self.view.addSubview(secondHengXiangFenGeXian!)
        self.view.addSubview(woYaoXueXiBtn!)
        
        hanZiXunLianBtn?.addTarget(self, action: #selector(hanZiXunLian), for: .touchUpInside)
        quWeiXunLianBtn?.addTarget(self, action: #selector(quWeiXunLian), for: .touchUpInside)
        dingZhiFangAnBtn?.addTarget(self, action: #selector(dingZhiFangAn), for: .touchUpInside)
        xiTongGuanLiBtn?.addTarget(self, action: #selector(xiTongGuanLi), for: .touchUpInside)
        woYaoXueXiBtn?.addTarget(self, action: #selector(woYaoXueZi), for: .touchUpInside)
        
    }

    override func viewWillAppear(_ animated: Bool) {
        self.title = "纵横码"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }
    
    func backToPrevious(){
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func hanZiXunLian(){
        let hanZiXunLianViewController = HanZiXunLianViewController()
        self.navigationController?.pushViewController(hanZiXunLianViewController, animated: true)
    }
    
    func quWeiXunLian(){
        let quWeiXunLianAnimationViewController = QuWeiXunLianAnimationViewController()
        self.navigationController?.pushViewController(quWeiXunLianAnimationViewController, animated: true)
    
    }
    
    func dingZhiFangAn(){
        let dingZhiFangAnViewController = DingZhiFangAnViewController()
        self.navigationController?.pushViewController(dingZhiFangAnViewController, animated: true)
    }
    
    func xiTongGuanLi(){
        let xiTongGuanLiViewController = XiTongGuanLiViewController()
        self.navigationController?.pushViewController(xiTongGuanLiViewController, animated: true)
    }
    
    func woYaoXueZi(){
        let woYaoXueXiViewController = WoYaoXueXiViewController()
        self.navigationController?.pushViewController(woYaoXueXiViewController, animated: true)
    }
    
    
    //封装一些属性方法
    func setAttribute(_ label:UILabel){
        label.font = UIFont(name: "Helvetica-Bold",size: 15)
    }
    
    //封装获取imageView的宽度的方法
    func getImageViewWidth(_ imageView:UIImageView) -> CGFloat{
        let imageViewWidth = imageView.bounds.size.width
        return imageViewWidth
    }
    
    //封装获取imageView的高度的方法
    func getImageViewHeight(_ imageView:UIImageView) -> CGFloat{
        let imageViewHeight = imageView.bounds.size.height
        return imageViewHeight
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
